package com.ruoyi.wechat.domain.threads;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.wechat.common.Constants;
import com.ruoyi.wechat.common.ServiceException;
import com.ruoyi.wechat.domain.*;
import com.ruoyi.wechat.mapper.AddrItemMapper;
import com.ruoyi.wechat.service.*;
import org.apache.http.HttpStatus;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class AfterOrderHandle implements Runnable {

    private final IProductService productService;

    private final IUserAddrService userAddrService;

    private final AddrItemMapper addrItemMapper;

    private final IOrderDelvService orderDelvService;

    private final IDelvTagService delvTagService;

    private final ISpecService specService;

    private final Order order;

    private final List<OrderItem> itemList;

    public AfterOrderHandle(IProductService productService, IUserAddrService userAddrService, AddrItemMapper addrItemMapper, IOrderDelvService orderDelvService, IDelvTagService delvTagService, ISpecService specService, Order order, List<OrderItem> itemList) {
        this.productService = productService;
        this.userAddrService = userAddrService;
        this.addrItemMapper = addrItemMapper;
        this.orderDelvService = orderDelvService;
        this.delvTagService = delvTagService;
        this.specService = specService;
        this.order = order;
        this.itemList = itemList;
    }

    @Override
    public void run() {
        if (Constants.Order.CATEGORY_1.getValue().equals(order.getCategory())) {
            //添加订单驿站标签
            UserAddr userAddr = userAddrService.getById(order.getAddrId());
            System.out.println("==============================================================================执行子线程");
            if (ObjectUtils.isEmpty(userAddr)) {
                throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "配送标签生成失败,orderId:" + order.getId());
            }
            //从地址项中获取站点集合
            AddrItem addrItem = addrItemMapper.selectById(userAddr.getAddrId());
            String labels = addrItem.getLabels();
            String[] sites = labels.split(",");
            List<DelvTag> delvTags = new ArrayList<>();

            //添加订单id对应的站点名称
            for (String site : sites) {
                DelvTag delvTag = new DelvTag();
                delvTag.setCategory(Constants.DelvLabel.LABEL_1);
                delvTag.setName(site);
                delvTag.setOrderId(order.getId());
                delvTags.add(delvTag);
            }
            //添加订单配送信息
            List<OrderDelv> delvList = new ArrayList<>();
            delvTagService.saveBatch(delvTags);
            for (OrderItem item : itemList) {
                LambdaQueryWrapper<Spec> specQuery = new LambdaQueryWrapper<>();
                specQuery.eq(Spec::getItemId,item.getProductId());
                List<Spec> specList = specService.list(specQuery);
                for (Spec spec : specList){
                    OrderDelv delv = new OrderDelv();
                    delv.setOrderId(order.getId());
                    delv.setProductId(spec.getItemId());
                    delv.setProductName(spec.getName());
                    delv.setProductPrice(spec.getPriceNormal());
                    delvList.add(delv);
                }
            }
            orderDelvService.saveBatch(delvList);
        }
    }
}
